package com.ucn.groupawesome.archgame;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class AndroidListViewActivity extends Activity
{
	ListView myList;
	Button getChoice;
	String selectedSeries;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.simple_list_item_multiple_choice);

		myList = (ListView) findViewById(R.id.list);
		getChoice = (Button) findViewById(R.id.getchoice);

		Bundle extras = getIntent().getExtras();

		if (extras != null)
		{
			selectedSeries = extras.getString("selectedSerie");
		}

		BrickManager bm = BrickManager.getInstance();
		CategoryManager cm = CategoryManager.getInstance();
		ArrayList<Brick> allAddedBricks = new ArrayList<Brick>();

		for (Category c : cm.getList())
		{
			for (Brick b : c.getListBricks())
			{
				allAddedBricks.add(b);

			}

		}

		ColorListAdapter adapter = new ColorListAdapter(this, android.R.layout.simple_list_item_multiple_choice, bm.getAllBricksExcept(allAddedBricks));

		myList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
		myList.setAdapter(adapter);

		getChoice.setOnClickListener(new Button.OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				BrickManager bm = BrickManager.getInstance();
				CategoryManager cm = CategoryManager.getInstance();

				Category selectedCat = cm.getCategory(selectedSeries);

				int cntChoice = myList.getCount();

				SparseBooleanArray sparseBooleanArray = myList.getCheckedItemPositions();

				for (int i = 0; i < cntChoice; i++)
				{
					if (sparseBooleanArray.get(i))
					{
						selectedCat.addBrick(bm.findBrick(myList.getItemAtPosition(i).toString()));
					}
				}

				Toast.makeText(AndroidListViewActivity.this, "Added to category", Toast.LENGTH_LONG).show();
				AndroidListViewActivity.this.finish();
			}
		});
	}

}
